package com.example.mydemo103.DB;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyDBHekper extends SQLiteOpenHelper {
    private  static final String DBNAME="yjz.db";
    private  static final int VERSION=1;
    public MyDBHekper(Context context){
        super(context,DBNAME,null,VERSION);

    }
    public MyDBHekper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String tbl_sql="create table if not exists tbl_user(uid integer primary key autoincrement,username varchar(30) not null,password varchar(8) not null,trueName varchar(10),sex varchar(2),\n" +
                "telephone varchar(11),birth  text,balance double,regTime text,role integer ,name varchar(30))";
        String addRecord="create table if not exists tbl_record(id integer primary key autoincrement,money double,"+
                "opUser varchar(20),opTime text)";
        SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");//设置日期格式
        String mydate=df.format(new Date());
        //创建表
        db.execSQL(tbl_sql);
        db.execSQL(addRecord);

//        db.execSQL("create table if not exists tbl_user(uid integer primary key autoincrement,username varchar(30) not null ,password varchar(8) not null,trueName varchar(10),sex varchar(2), telephone varchar(11),birth text,balance double,ragTime text,role integer)");
//        db.execSQL("create table if not exists tbl_record(id integer primary key autoincrement,money double,opUser varchar(20),opTime text)");
        //插入初始值
        db.execSQL("insert into tbl_user(username,password,sex,telephone,balance,regTime,role,name)values('admin','123','男','13065162873','25','2021-5-19',0,'管理员')");
        db.execSQL("insert into tbl_user(username,password,sex,telephone,balance,regTime,role,name)values('yjz','103','男','13065162873','25','2021-5-19',1,'余吉钊')");
        db.execSQL("insert into tbl_record(money,opUser,opTime) values(50.0,'admin','"+mydate+"')");
       db.execSQL("insert into tbl_record(money,opUser,opTime) values(20.0,'admin','"+mydate+"')");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
